module.exports = () => {
  return async (ctx, next) => {
    const authorization = ctx.get('Authorization') || ctx.get('authorization') || ctx.cookies.get('authorization');
    if (!authorization) {
      ctx.body = {
        code: 50001,
        data: '',
        msg: 'token 缺失',
      };
    } else {
      const token = authorization.split(' ')[1];
      const res = ctx.service.user.verify(token);
      if (res.status === 1) {
        ctx.state.user = res.data;
        // console.log('ctx.state.user: ', ctx.state.user);
        await next();
      } else {
        ctx.body = {
          code: 50001,
          data: res.status,
          msg: res.data,
        };
      }
    }
  };
};
